package com.delio.client.produccion.notaingresos;

import java.util.List;

import com.extjs.gxt.ui.client.data.BaseFilterPagingLoadConfig;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.FilterPagingLoadConfig;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class NotaIngresos {
	 public static NotaIngresoServiceAsync nis = GWT.create(NotaIngresoService.class);;
	 public static PagingLoader<PagingLoadResult<ModelData>> loader;
	 public static ListStore<BeanModel> store;
	 public static ListStore<ModelData> storeCombo;
	 public static ListLoader<ListLoadResult<ModelData>> loaderCombo;
	 public static Integer pedido_id,detallepedido_id; 
		
	 public static void getPaginate() {
			RpcProxy<PagingLoadResult<NotaIngreso>> proxy = new RpcProxy<PagingLoadResult<NotaIngreso>>() {

				@Override
				protected void load(Object loadConfig,
				      AsyncCallback<PagingLoadResult<NotaIngreso>> callback) {
					nis.paginateNotaIngreso((FilterPagingLoadConfig) loadConfig, callback);  
				}  
		    };
		    loader = new BasePagingLoader<PagingLoadResult<ModelData>>(proxy, new BeanModelReader()){  
		        @Override  
		        protected Object newLoadConfig() {  
		          BasePagingLoadConfig config = new BaseFilterPagingLoadConfig();  
		          return config;  
		        }  
		      };  
	        loader.setRemoteSort(true);  
	        store = new ListStore<BeanModel>(loader);
	 }
	 
	 
public static void getStore(){
		
	 RpcProxy<List<NotaIngreso>> proxy = new RpcProxy<List<NotaIngreso>>(){
	    protected void load(Object loadConfig, AsyncCallback<List<NotaIngreso>> callback){
		    	nis.ListarNotaIngresos(callback);
		 }
	  	};
	 loaderCombo = new BaseListLoader<ListLoadResult<ModelData>>(proxy, new BeanModelReader());
	 storeCombo = new ListStore<ModelData>(loaderCombo);
	 loaderCombo.load();
	  
  }
/*
 public static void getId(){
	 nis.getId(new  AsyncCallback<Integer>(){
		@Override
		public void onFailure(Throwable caught) {
			MessageBox.alert("Error", "No pudo guardar", null);
		}
		@Override
		public void onSuccess(Integer result) {
			op_id =result;
		}
	 });
 }*/
 
 
 public static void guardar(NotaIngreso ni){
	 
	 nis.guardar(ni, new AsyncCallback<Boolean>(){

		@Override
		public void onFailure(Throwable caught) {
			MessageBox.alert("Error", "No pudo guardar", null);			
		}

		@Override
		public void onSuccess(Boolean result) {
          if(!result)
            MessageBox.alert("Error", "No se guardo correctamente", null);
    	}
	 
	 });
 }
 
 public static void actualizar(NotaIngreso ni){
	 nis.actualizar(ni, new AsyncCallback<Boolean>(){

	   @Override
	   public void onFailure(Throwable caught) {
		   MessageBox.alert("Error", "Error en la acutalizacion", null);	 
	   }	
		 
	   public void onSuccess(Boolean result){
		 if(result){
		   MessageBox.alert("Actualizado","Se actualizo Correctamente", null); 
		 }else{
		   MessageBox.alert("Error", "No se actualizo Correctament", null);	 
		 }
		}

	 });
 }
 
 public static void eliminar(int id){
	 nis.eliminar(id, new AsyncCallback<Boolean>(){
		
		 @Override
		 public void onFailure(Throwable caught){
			 MessageBox.alert("Error", "Error en la Eliminacion", null);
		 }
		 
		 public void onSuccess(Boolean result){
			 if(result){
			   MessageBox.alert("Eliminado","Se elimino Correctamente", null); 
			 }else{
			   MessageBox.alert("Error", "No se elimino Correctamente", null);	 
			 } 
		 }

	 });
 }

}
